#coding:utf-8
import bottle
from google.appengine.ext.webapp import util
from mako.template import Template
from bottle import route, get, post, redirect, static_file
from guestbook import list_entry, add_entry
from blog import Blog
from admin import Admin
from config import get_webpage_strings


#调试开启
bottle.debug(True)

@route('/:routes#.+#/fail')
@route('/:routes#.+#/fail/')
def post_fail(routes):
    #此处留待修改，加入主题功能后修改此处
    webpage = get_webpage_strings(now=u'失败')
    return Template(filename='./theme/'+webpage['theme']+'/templates/fail.tpl', input_encoding='utf-8', output_encoding='utf-8').render(url=routes)

@route('/:routes#.+#/success')
@route('/:routes#.+#/success/')
def configsuccess(routes):
    redirect('/'+routes)
    
@route('/:routes#.+#/None')
def nonepage(routes):
    redirect('/'+routes)
## 以下是静态文件

@route('/templates/:path#.+#')
def templates(path):
    webpage = get_webpage_strings(now=u'失败')
    return static_file(path, root='./theme/'+webpage['theme']+'/templates/')

@route('/admin/:path#.+#')
def admincptemp(path):
    return static_file(path, root='./admin')

@route('/editor/:path#.+#')
def onlineeditor(path):
    return static_file(path, root='./editor')

## 以上是静态文件

## 以下是留言板

@post('/guestbook')
@post('/guestbook/')
@post('/guestbook/page/:num#[0-9]{0,2}#')
@post('/guestbook/page/:num#[0-9]{0,2}#/')
def add_entrys(num=1):
    return add_entry(baseurl='guestbook/')

@get('/guestbook')
@get('/guestbook/')
@get('/guestbook/page/:num#[0-9]{0,2}#')
@get('/guestbook/page/:num#[0-9]{0,2}#/')
def list_entrys(num=1):
    return list_entry(num)

## 以上是留言板

## 以下是博客

@route('/blog/')
@route('/blog')
@route('/blog/page/:num#[0-9]{0,2}#')
@route('/blog/page/:num#[0-9]{0,2}#/')
def blog_views(num=1):
    blog = Blog()
    return blog.view('blog', 0, num)

@get('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url')
@get('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.html')
@get('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.php')
@get('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.asp')
@get('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.aspx')
@get('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.py')
def blog_details(year, month, day, url):
    blog = Blog()
    return blog.detial(year, month, day, url)

@post('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url')
@post('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.html')
@post('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.php')
@post('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.asp')
@post('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.aspx')
@post('/blog/:year#[0-9]{4}#/:month#[0-9]{1,2}#/:day#[0-9]{1,2}#/:url.py')
def add_commtents(year, month, day, url):
    blog = Blog()
    return blog.add_comment(year, month, day, url, baseurl='/blog/')

@route('/blog/:typename/:value')
@route('/blog/:typename/:value/')
@route('/blog/:typename/:value/page/:num#[0-9]{1,2}#')
@route('/blog/:typename/:value/page/:num#[0-9]{1,2}#/')
def type_views(typename, value, num=1):
    blog = Blog()
    return blog.view(typename, value.decode('UTF-8'), num)

## 以上是博客

## 以下是系统管理

@route('/admincp')
@route('/admincp/')
def admincp():
    redirect('/admincp/config')

@get('/admincp/config')
@get('/admincp/config/')
def admin_get_configs():
    admin = Admin('config')
    return admin.config()
    
@post('/admincp/config')
@post('/admincp/config/')
def admin_change_configs():
    admin = Admin('config')
    return admin.accept_config()

@get('/admincp/:cmd#[a-zA-Z]+#')
@get('/admincp/:cmd#[a-zA-Z]+#/')
@get('/admincp/:cmd#[a-zA-Z]+#/page/:num#[0-9]{0,2}#')
@get('/admincp/:cmd#[a-zA-Z]+#/page/:num#[0-9]{0,2}#/')
def admin_list_views(cmd, num=1):
    admin = Admin(cmdtype=cmd)
    return admin.list(num)

@post('/admincp/:cmd#[a-zA-Z]+#')
@post('/admincp/:cmd#[a-zA-Z]+#/')
@post('/admincp/:cmd#[a-zA-Z]+#/page/:num#[0-9]{0,2}#')
@post('/admincp/:cmd#[a-zA-Z]+#/page/:num#[0-9]{0,2}#/')
def admin_list_views2(cmd, num=1):
    admin = Admin(cmdtype=cmd)
    return admin.change()

@get('/admincp/:cmd/edit/:key')
@get('/admincp/:cmd/edit/:key/')
def admin_edits(cmd, key):
    admin = Admin(cmdtype=cmd)
    return admin.edit(key)

@post('/admincp/:cmd/edit/:key')
@post('/admincp/:cmd/edit/:key/')
def admin_accept_edits(cmd, key):
    admin = Admin(cmdtype=cmd)
    return admin.accept_edit(key)

@route('/admincp/:cmd/change/:key')
@route('/admincp/:cmd/change/:key/')
def admin_accept_changes(cmd, key):
    admin = Admin(cmdtype=cmd)
    return admin.accept_change(key)

@get('/admincp/blog/post')
@get('/admincp/blog/post/')
def admin_blog_posts():
    admin = Admin(cmdtype='blog')
    return admin.post()

@post('/admincp/blog/post/')
@post('/admincp/blog/post')
def admin_blog_postteds():
    admin = Admin(cmdtype='blog')
    return admin.accept_edit(0)
## 以上是系统管理

@route('/')
def homepage():
    #首页直接跳转到博客
    redirect('blog/')

util.run_wsgi_app(bottle.default_app())